import 'package:citiesocial/_include.dart';
import 'package:citiesocial/widgets/goods/order_item.dart';
import 'package:citiesocial/ui/account/order_detail.dart';

class MyOrders extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _MyOrdersState();
}

class _MyOrdersState extends BaseState<MyOrders> {
  var _page = 1, _status = 0;
  var _loadable = true;
  static const _types = const ['全部', '待付款', '待收货', '已完成', '已取消'];

  final _list = List<OrderEntity>();

  void _selectTab(int index) {
    if (_status != index) {
      _loadable = true;
      _page = 1;
      setState(() {
        _status = index;
      });
      _loadData();
    }
  }

  void _loadData() {
    LoadingDialog.show(context);
    HttpManager.orderAll(Configs.userId, Configs.userToken, '$_status',
            page: _page, context: context)
        .then((list) {
      LoadingDialog.dismiss(context);
      if (_page <= 1) {
        _list.clear();
      }
      if (list != null && list.length > 0) {
        _loadable = list.length >= HttpManager.default_size;
        _list.addAll(list);
      } else {
        _loadable = false;
      }
      setState(() {});
    });
  }

  void _cancel(orderId) {
    LoadingDialog.show(context);
    HttpManager.cancel(Configs.userId, Configs.userToken, orderId).then((yes) {
      LoadingDialog.dismiss(context);
      if (yes) {
        _loadable = true;
        _page = 1;
        _loadData();
      }
    });
  }

  void _goPay(orderId) {}

  void _confirmReceipt(orderId) {
    LoadingDialog.show(context);
    HttpManager.sureGoods(Configs.userId, Configs.userToken, orderId)
        .then((yes) {
      LoadingDialog.dismiss(context);
      if (yes) {
        _loadable = true;
        _page = 1;
        _loadData();
      }
    });
  }

  @override
  void initState() {
    Future.delayed(Configs.loadDelay, () {
      _loadData();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBarUtils.normal(context, '全部订单'),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: Column(
            children: <Widget>[
              SingleChildScrollView(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  mainAxisSize: MainAxisSize.max,
                  children: List.generate(
                    _types.length,
                    (index) => Expanded(
                          child: GestureDetector(
                            child: Container(
                              color: Colors.white,
                              child: Center(
                                child: Text(
                                  _types[index],
                                  style: TextStyle(
                                      fontSize: 13.0,
                                      color: _status == index
                                          ? Configs.main
                                          : Colors.black),
                                ),
                              ),
                              padding: EdgeInsets.all(15.0),
                            ),
                            onTap: () => _selectTab(index),
                          ),
                          flex: 1,
                        ),
                  ),
                ),
              ),
              Expanded(
                child: DListView(
                  onRefresh: () async {
                    _loadable = true;
                    _page = 1;
                    _loadData();
                  },
                  onLoadmore: () {
                    if (_loadable) {
                      _page++;
                      _loadData();
                    }
                  },
                  itemBuilder: (_, index) =>
                      OrderItem(_list[index], (order, type) {
                        if (type == ClickType.CANCEL) {
                          _cancel(order.order_id);
                        } else if (type == ClickType.PAY) {
                          _goPay(order.order_id);
                        } else if (type == ClickType.CINFIRM_RECEIPT) {
                          _confirmReceipt(order.order_id);
                        } else if (type == ClickType.ITEM) {
                          Navigator.push(
                              context,
                              MaterialPageRoute(
                                  builder: (context) =>
                                      OrderDetail(order.order_id)));
                        }
                      }),
                  itemCount: _list.length,
                ),
              )
            ],
          ),
        ),
      );
}
